package allocator;

import java.util.Random;

/**
 * {@link PageRequestor} is a simple implementation of {@link Runnable} that uses a {@link MemoryAllocator}
 * to request and release pages. 
 *
 */
public class PageRequestor implements Runnable {

	private IMemoryAllocator allocator;
	private Random random;

	public PageRequestor(IMemoryAllocator allocator) {
		this.allocator = allocator;
		this.random = new Random();
	}

	@Override
	public void run() {
		try {
			while (true) {
				int numberOfPagesToRequest = random.nextInt(allocator.getTotalAmount());
				allocator.request(numberOfPagesToRequest);
				Thread.sleep(2000);	// putting thread to sleep to allow other threads a chance to request pages
				allocator.release(numberOfPagesToRequest);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}
